<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/include/taglib.jsp"%>
<html>
<head>
<meta charset="UTF-8">
<title>我要投资-
	<s:if test="%{#request.debtDetailMap.borrowProduct == 2}">月利宝详情</s:if>
	<s:elseif test="%{#request.debtDetailMap.borrowProduct == 3}">季发宝详情</s:elseif>
	<s:elseif test="%{#request.debtDetailMap.borrowProduct == 4}">稳盈宝详情</s:elseif>
|项目转让</title>
<jsp:include page="/include/wchat/head.jsp"></jsp:include>
<link type="text/css" rel="stylesheet" href="/v2/wchat/resrc/css/invest.css">
</head>
<body>
<div id="header"> <a href="wchatDebtDetail.do?id=${debtDetailMap.id}" class="backto">返回</a>
  <div class="title-sub"><s:if test="%{#request.debtDetailMap.borrowProduct == 2}">月利宝详情</s:if>
		  <s:elseif test="%{#request.debtDetailMap.borrowProduct == 3}">季发宝详情</s:elseif>
		  <s:elseif test="%{#request.debtDetailMap.borrowProduct == 4}">稳盈宝详情</s:elseif></div>
</div>
<div id="wrap">
  <div class="loan-transfer">
    <div class="invest-detail"><b class="icon-qiang"></b>
      <h1>
          <s:if test="%{#request.debtDetailMap.borrowProduct == 2}">月利宝  ${debtDetailMap.SN}</s:if>
		  <s:elseif test="%{#request.debtDetailMap.borrowProduct == 3}">季发宝  ${debtDetailMap.SN}</s:elseif>
		  <s:elseif test="%{#request.debtDetailMap.borrowProduct == 4}">稳盈宝  ${debtDetailMap.borrowNumber}</s:elseif>
      </h1>
      <ul class="items">
        <li class="c1">可购金额</li>
        <li class="c2">￥${debtDetailMap.debtSum}</li>
      </ul>
      <ul class="items">
        <li class="c1">账户余额</li>
        <li class="c2 per">￥<s:if test="#request.accmountStatisMap.usableSum !=''">${accmountStatisMap.usableSum}</s:if><s:else>0</s:else>
        	<input type="hidden" id="realnameFlag" value="${session.user.isBindIdNo}">
        </li>
      </ul>
      <ul class="items">
        <li class="c1">安全保障</li>
        <li class="c2 safe">100%本息保障</li>
      </ul>
    </div>
    <div class="invest-buy">
      <ul>
        <li class="verify"> <span class="verify-code">
          <input type="text" class="input" maxlength="4" id="authCode" placeholder="请输入验证码">
          </span><img src="I4008lOve893gOfUll983/imageCode.do?pageId=debtWchatInvest" onclick="coderefresh()" id="verifyCodeImg"> </li>
        <li class="argee">
          <input id="agreeContract" type="checkbox" class="ipt-chk" checked="checked">
          我同意《<a href="/getMessageBytypeId.do?typeId=22">共富网债权转让协议</a>》</li>
      </ul>
      <div class="vtip" id="errorTip"></div>
    </div>
    <div class="operate-button operate-buy"> <a href="javascript:buyCredit();" class="btn">我要购买</a> </div>
  </div>
</div>

<jsp:include page="/include/wchat/footer.jsp"></jsp:include>
<script type="text/javascript">
$(function(){
  $('.ipt-chk').click(function(){
    var agreement = $('.ipt-chk');
    if (agreement.attr("checked") == "checked") {
      agreement.removeAttr("checked");
    } else {
      agreement.attr("checked", "checked");
    }
  });
});
//刷新验证码
function coderefresh() {
    var verifyCode = $("#verifyCodeImg");
    var now = new Date();
    verifyCode.attr("src",'I4008lOve893gOfUll983/imageCode.do?pageId=debtWchatInvest&d=' + now.getTime());
}
//购买债权
function buyCredit() {
  var user = '${session.user}';
  var loanInvestorId = ${debtDetailMap.id};
  var soldPrice = Number(${debtDetailMap.debtSum});
  var userCash = Number(${accmountStatisMap.usableSum});
  var forbidStatus = Number(${debtDetailMap.debtStatus});
  var authCode = $('#authCode').val();
  var errorTip = $('#errorTip');

  if (user == null || user == '') {
    window.location.href="/wchatLoginInit.do";
    return;
  };
  if(forbidStatus != 2){
    errorTip.html('抱歉，您当前不能购买债权');
    return;
  };
  if($.trim(authCode) == ''){
    errorTip.html('请输入验证码');
    return;
  };
  if(userCash < soldPrice){
    errorTip.html('您的余额不足，请充值');
    return;
  };
  if($('#agreeContract').attr("checked") != "checked"){
  	errorTip.html('请同意共富网投资管理协议');
    return false;
  }
  var param = {};
  param['paramMap.debtId'] = loanInvestorId;
  param["paramMap.pageId"] = "debtWchatInvest";
  param["paramMap.code"] = authCode;
  $.ajax({
    dataType: 'json',
    url: '/buyCredit.do',
    type: 'POST',
    data: param,
    beforeSend:function() {
    },
    success:function(data) {
    	if(data.msg == '验证码错误') {
            errorTip.html('验证码错误');
            switchCode();
        }else if(data.msg == "success"){
    	  alert("恭喜您，购买成功！");
        window.location.href="/wchatDebtDetail.do?id="+loanInvestorId;
      }else{
    	  coderefresh();
          errorTip.html(data.msg);
      }
    },
    error: function() {
      coderefresh();
      errorTip.html("购买失败，请刷新页面或稍后再试");
    }
  });
}
</script>
</body>
</html>